﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using XF.IDAL;
using XF.DBUtility;//Please add references
using System.Collections;
using XF.Common;

namespace XF.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Bill_DTOItems
	/// </summary>
	public partial class Bill_DTOItems:IBill_DTOItems
	{
		public Bill_DTOItems()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SqlServerHelper.GetMaxID("Id", "Bill_DTOItems"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Bill_DTOItems");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return SqlServerHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XF.Model.Bill_DTOItems model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Bill_DTOItems(");
			strSql.Append("OrderCode,Item,MaterielId,Unit,Qty,Price,STOItemId,Remark,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable,Paid)");
			strSql.Append(" values (");
			strSql.Append("@OrderCode,@Item,@MaterielId,@Unit,@Qty,@Price,@STOItemId,@Remark,@CreateDate,@CreateUser,@LastUpdateDate,@LastUpdateUser,@Enable,@Paid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@Item", SqlDbType.Int,4),
					new SqlParameter("@MaterielId", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.Int,4),
					new SqlParameter("@Qty", SqlDbType.Decimal,9),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@STOItemId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@CreateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1),
					new SqlParameter("@Paid", SqlDbType.Decimal,9)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.Item;
			parameters[2].Value = model.MaterielId;
			parameters[3].Value = model.Unit;
			parameters[4].Value = model.Qty;
			parameters[5].Value = model.Price;
			parameters[6].Value = model.STOItemId;
			parameters[7].Value = model.Remark;
			parameters[8].Value = model.CreateDate;
			parameters[9].Value = model.CreateUser;
			parameters[10].Value = model.LastUpdateDate;
			parameters[11].Value = model.LastUpdateUser;
			parameters[12].Value = model.Enable;
			parameters[13].Value = model.Paid;

			object obj = SqlServerHelper.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XF.Model.Bill_DTOItems model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Bill_DTOItems set ");
			strSql.Append("OrderCode=@OrderCode,");
			strSql.Append("Item=@Item,");
			strSql.Append("MaterielId=@MaterielId,");
			strSql.Append("Unit=@Unit,");
			strSql.Append("Qty=@Qty,");
			strSql.Append("Price=@Price,");
			strSql.Append("STOItemId=@STOItemId,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("LastUpdateDate=@LastUpdateDate,");
			strSql.Append("LastUpdateUser=@LastUpdateUser,");
			strSql.Append("Enable=@Enable,");
			strSql.Append("Paid=@Paid");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@Item", SqlDbType.Int,4),
					new SqlParameter("@MaterielId", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.Int,4),
					new SqlParameter("@Qty", SqlDbType.Decimal,9),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@STOItemId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1),
					new SqlParameter("@Paid", SqlDbType.Decimal,9),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.Item;
			parameters[2].Value = model.MaterielId;
			parameters[3].Value = model.Unit;
			parameters[4].Value = model.Qty;
			parameters[5].Value = model.Price;
			parameters[6].Value = model.STOItemId;
			parameters[7].Value = model.Remark;
			parameters[8].Value = model.LastUpdateDate;
			parameters[9].Value = model.LastUpdateUser;
			parameters[10].Value = model.Enable;
			parameters[11].Value = model.Paid;
			parameters[12].Value = model.Id;

			int rows= SqlServerHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Bill_DTOItems ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows= SqlServerHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Bill_DTOItems ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows= SqlServerHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_DTOItems GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 * from Bill_DTOItems ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			XF.Model.Bill_DTOItems model=new XF.Model.Bill_DTOItems();
			DataSet ds= SqlServerHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_DTOItems DataRowToModel(DataRow row)
		{
			XF.Model.Bill_DTOItems model = new XF.Model.Bill_DTOItems();
			if (row != null)
			{
				if (row["Id"] != null && row["Id"].ToString() != "")
				{
					model.Id = int.Parse(row["Id"].ToString());
				}
				if (row["OrderCode"] != null)
				{
					model.OrderCode = row["OrderCode"].ToString();
				}
				if (row["Item"] != null && row["Item"].ToString() != "")
				{
					model.Item = int.Parse(row["Item"].ToString());
				}
				if (row["MaterielId"] != null && row["MaterielId"].ToString() != "")
				{
					model.MaterielId = int.Parse(row["MaterielId"].ToString());
				}
				if (row["Unit"] != null && row["Unit"].ToString() != "")
				{
					model.Unit = int.Parse(row["Unit"].ToString());
				}
				if (row["Qty"] != null && row["Qty"].ToString() != "")
				{
					model.Qty = decimal.Parse(row["Qty"].ToString());
				}
				if (row["Price"] != null && row["Price"].ToString() != "")
				{
					model.Price = decimal.Parse(row["Price"].ToString());
				}
				if (row["STOItemId"] != null && row["STOItemId"].ToString() != "")
				{
					model.STOItemId = int.Parse(row["STOItemId"].ToString());
				}
				if (row["Remark"] != null)
				{
					model.Remark = row["Remark"].ToString();
				}
				if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
				{
					model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
				}
				if (row["CreateUser"] != null)
				{
					model.CreateUser = row["CreateUser"].ToString();
				}
				if (row["LastUpdateDate"] != null && row["LastUpdateDate"].ToString() != "")
				{
					model.LastUpdateDate = DateTime.Parse(row["LastUpdateDate"].ToString());
				}
				if (row["LastUpdateUser"] != null)
				{
					model.LastUpdateUser = row["LastUpdateUser"].ToString();
				}
				if (row["Enable"] != null && row["Enable"].ToString() != "")
				{
					if ((row["Enable"].ToString() == "1") || (row["Enable"].ToString().ToLower() == "true"))
					{
						model.Enable = true;
					}
					else
					{
						model.Enable = false;
					}
				}
				if (row["Paid"] != null && row["Paid"].ToString() != "")
				{
					model.Paid = decimal.Parse(row["Paid"].ToString());
				}
				#region 扩展属性
				if (row.Table.Columns.Contains("DTOOrderId"))
				{
					if (row["DTOOrderId"] != null && row["DTOOrderId"].ToString() != "")
					{
						model.DTOOrderId = int.Parse(row["DTOOrderId"].ToString());
					}
				}

				if (row.Table.Columns.Contains("OrderDate"))
				{
					if (row["OrderDate"] != null && row["OrderDate"].ToString() != "")
					{
						model.OrderDate = DateTime.Parse(row["OrderDate"].ToString());
					}
				}
				if (row.Table.Columns.Contains("CustomerId"))
				{
					if (row["CustomerId"] != null && row["CustomerId"].ToString() != "")
					{
						model.CustomerId = int.Parse(row["CustomerId"].ToString());
					}
				}

				if (row.Table.Columns.Contains("CustomerCode"))
				{
					if (row["CustomerCode"] != null)
					{
						model.CustomerCode = row["CustomerCode"].ToString();
					}
				}
				if (row.Table.Columns.Contains("CustomerName"))
				{
					if (row["CustomerName"] != null)
					{
						model.CustomerName = row["CustomerName"].ToString();
					}
				}
				if (row.Table.Columns.Contains("STOOrderCode"))
				{
					if (row["STOOrderCode"] != null)
					{
						model.STOOrderCode = row["STOOrderCode"].ToString();
					}
				}
				if (row.Table.Columns.Contains("STOQty"))
				{
					if (row["STOQty"] != null && row["STOQty"].ToString() != "")
					{
						model.STOQty = decimal.Parse(row["STOQty"].ToString());
					}
				}
				if (row.Table.Columns.Contains("MaterielName"))
				{
					if (row["MaterielName"] != null)
					{
						model.MaterielName = row["MaterielName"].ToString();
					}
				}
				if (row.Table.Columns.Contains("MaterielModel"))
				{
					if (row["MaterielModel"] != null)
					{
						model.MaterielModel = row["MaterielModel"].ToString();
					}
				}
				if (row.Table.Columns.Contains("Status"))
				{
					if (row["Status"] != null && row["Status"].ToString() != "")
					{
						model.Status = int.Parse(row["Status"].ToString());
					}
				}
				if (row.Table.Columns.Contains("PayAmount"))
				{
					if (row["PayAmount"] != null && row["PayAmount"].ToString() != "")
					{
						model.PayAmount = decimal.Parse(row["PayAmount"].ToString());
					}
				}
				if (row.Table.Columns.Contains("PayType"))
				{
					if (row["PayType"] != null && row["PayType"].ToString() != "")
					{
						model.PayType = int.Parse(row["PayType"].ToString());
					}
				}
				if (row.Table.Columns.Contains("OrderRemark"))
				{
					if (row["OrderRemark"] != null)
					{
						model.OrderRemark = row["OrderRemark"].ToString();
					}
				}
				#endregion
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Bill_DTOItems ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM Bill_DTOItems ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Bill_DTOItems ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SqlServerHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from Bill_DTOItems T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SqlServerHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Bill_DTOItems";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
		// <summary>
		/// 获取分页数据
		/// </summary>
		/// <param name="strWhere"></param>
		/// <param name="strOrder"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public DataTable GetListByPage(string strWhere, string strOrder, int pageIndex, int pageSize, ref int count)
		{
			string tbName = " V_Bill_DTOItems A ";
			string tbGetFields = " A.* ";
			return SqlServerHelper.GetPageList(tbName, tbGetFields, strOrder, pageSize, pageIndex, strWhere, ref count);
		}

		public DataTable GetDetailListByPage(string strWhere, string strOrder, int pageIndex, int pageSize, ref int count)
		{
			string tbName = " V_Bill_DTOItems A ";
			string tbGetFields = " A.* ";
			return SqlServerHelper.GetPageList(tbName, tbGetFields, strOrder, pageSize, pageIndex, strWhere, ref count);
		}

		public DictionaryEntry GetAddSql(XF.Model.Bill_DTOItems model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Bill_DTOItems(");
			strSql.Append("OrderCode,Item,MaterielId,Unit,Qty,Price,STOItemId,Remark,CreateDate,CreateUser,LastUpdateDate,LastUpdateUser,Enable,Paid)");
			strSql.Append(" values (");
			strSql.Append("@OrderCode,@Item,@MaterielId,@Unit,@Qty,@Price,@STOItemId,@Remark,@CreateDate,@CreateUser,@LastUpdateDate,@LastUpdateUser,@Enable,@Paid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@Item", SqlDbType.Int,4),
					new SqlParameter("@MaterielId", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.Int,4),
					new SqlParameter("@Qty", SqlDbType.Decimal,9),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@STOItemId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@CreateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1),
					new SqlParameter("@Paid", SqlDbType.Decimal,9)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.Item;
			parameters[2].Value = model.MaterielId;
			parameters[3].Value = model.Unit;
			parameters[4].Value = model.Qty;
			parameters[5].Value = model.Price;
			parameters[6].Value = model.STOItemId;
			parameters[7].Value = model.Remark;
			parameters[8].Value = model.CreateDate;
			parameters[9].Value = model.CreateUser;
			parameters[10].Value = model.LastUpdateDate;
			parameters[11].Value = model.LastUpdateUser;
			parameters[12].Value = model.Enable;
			parameters[13].Value = model.Paid;

			return new DictionaryEntry(strSql.ToString(), parameters);
		}

		public DictionaryEntry GetUpdateSql(XF.Model.Bill_DTOItems model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Bill_DTOItems set ");
			strSql.Append("OrderCode=@OrderCode,");
			strSql.Append("Item=@Item,");
			strSql.Append("MaterielId=@MaterielId,");
			strSql.Append("Unit=@Unit,");
			strSql.Append("Qty=@Qty,");
			strSql.Append("Price=@Price,");
			strSql.Append("STOItemId=@STOItemId,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("LastUpdateDate=@LastUpdateDate,");
			strSql.Append("LastUpdateUser=@LastUpdateUser,");
			strSql.Append("Enable=@Enable,");
			strSql.Append("Paid=@Paid");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64),
					new SqlParameter("@Item", SqlDbType.Int,4),
					new SqlParameter("@MaterielId", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.Int,4),
					new SqlParameter("@Qty", SqlDbType.Decimal,9),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@STOItemId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@LastUpdateDate", SqlDbType.DateTime),
					new SqlParameter("@LastUpdateUser", SqlDbType.NVarChar,127),
					new SqlParameter("@Enable", SqlDbType.Bit,1),
					new SqlParameter("@Paid", SqlDbType.Decimal,9),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.OrderCode;
			parameters[1].Value = model.Item;
			parameters[2].Value = model.MaterielId;
			parameters[3].Value = model.Unit;
			parameters[4].Value = model.Qty;
			parameters[5].Value = model.Price;
			parameters[6].Value = model.STOItemId;
			parameters[7].Value = model.Remark;
			parameters[8].Value = model.LastUpdateDate;
			parameters[9].Value = model.LastUpdateUser;
			parameters[10].Value = model.Enable;
			parameters[11].Value = model.Paid;
			parameters[12].Value = model.Id;

			return new DictionaryEntry(strSql.ToString(), parameters);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public DictionaryEntry GetDeleteSql(string orderCode)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from Bill_DTOItems ");
			strSql.Append(" where OrderCode=@OrderCode");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,64)
			};
			parameters[0].Value = orderCode;


			return new DictionaryEntry(strSql.ToString(), parameters);
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetDetailList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM V_Bill_DTOItems ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			return SqlServerHelper.Query(strSql.ToString());
		}

		public decimal GetLastPrice(int customerId,int materielId)
        {
			string strSql = string.Format("select dbo.Func_GetLastPrice({0},{1})", customerId, materielId);
			object obj = SqlServerHelper.GetSingle(strSql);
			return zDataConverter.ToDecimal(obj);
		}

		public DataSet GetSaleStatementUnclean(int iCustomerId)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select *,Qty * Price as TotlePrice from V_SaleStatement ");
			strSql.Append("where CONVERT(varchar(10), OrderDate, 23) > (select ISNULL(MAX(CheckOutDate), '2020-01-01') from Base_CheckOut where CustomerId = " + iCustomerId + ") ");
			strSql.Append("and Status <> 3 ");
			strSql.Append("and CustomerId = " + iCustomerId);
			strSql.Append(" Order By OrderDate Asc");
			return SqlServerHelper.Query(strSql.ToString());
		}

		public DataSet GetSaleStatementUnclean(int iCustomerId, DateTime dateCheckOut)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select *,Qty * Price as TotlePrice from V_SaleStatement ");
			strSql.Append("where CONVERT(varchar(10), OrderDate, 23) > (select ISNULL(MAX(CheckOutDate), '2020-01-01') from Base_CheckOut where CustomerId = " + iCustomerId + ") ");
			strSql.Append("and CustomerId = " + iCustomerId);
			strSql.Append("and CONVERT(varchar(10), OrderDate, 23) <= '" + dateCheckOut.ToString("yyyy-MM-dd") + "'");
			strSql.Append(" Order By OrderDate Asc");
			return SqlServerHelper.Query(strSql.ToString());
		}

		public DataSet GetCheckOutData(int iCustomerId, DateTime dateCheckOut)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select ISNULL(SUM(Qty * Price),0) as TotlePrice,ISNULL(SUM(Paid),0) as Paid from V_Bill_DTOItems ");
			strSql.Append("where CONVERT(varchar(10), CreateDate, 23) > (select ISNULL(MAX(CheckOutDate), '2020-01-01') from Base_CheckOut where CustomerId = " + iCustomerId + ") ");
			strSql.Append("and CustomerId = " + iCustomerId);
			strSql.Append("and CONVERT(varchar(10), CreateDate, 23) <= '" + dateCheckOut.ToString("yyyy-MM-dd") + "' ");
			return SqlServerHelper.Query(strSql.ToString());
		}

		public DataSet GetSaleStatement(int iCustomerId)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append(" select * from V_SaleStatementHistory ");
			strSql.Append(" where CustomerId = " + iCustomerId);
			strSql.Append(" Order By OrderDate Asc");
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取历史订单
		/// </summary>
		/// <param name="strWhere"></param>
		/// <returns></returns>
		public DataSet GetHistoryList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select distinct MaterielId,MaterielName,MaterielModel,Unit,Qty,Price,Remark ");
			strSql.Append("from V_Bill_DTOItems ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" Where " + strWhere);
			}
			return SqlServerHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 更新实收数据
		/// </summary>
		public bool UpdatePaid(int id,decimal paid)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Bill_DTOItems set ");
			strSql.Append("Paid=@Paid ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Paid", SqlDbType.Decimal,9),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = paid;
			parameters[1].Value = id;

			int rows = SqlServerHelper.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.V_SaleStatement ToStatementModel(DataRow row)
		{
			XF.Model.V_SaleStatement model = new XF.Model.V_SaleStatement();
			if (row != null)
			{
				if (row.Table.Columns.Contains("RecordType"))
				{
					if (row["RecordType"] != null && row["RecordType"].ToString() != "")
					{
						model.RecordType = int.Parse(row["RecordType"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Id"))
				{
					if (row["Id"] != null && row["Id"].ToString() != "")
					{
						model.Id = int.Parse(row["Id"].ToString());
					}
				}
				if (row.Table.Columns.Contains("CustomerId"))
				{
					if (row["CustomerId"] != null && row["CustomerId"].ToString() != "")
					{
						model.CustomerId = int.Parse(row["CustomerId"].ToString());
					}
				}
				if (row.Table.Columns.Contains("OrderDate"))
				{
					if (row["OrderDate"] != null && row["OrderDate"].ToString() != "")
					{
						model.OrderDate = DateTime.Parse(row["OrderDate"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Materiel"))
				{
					if (row["Materiel"] != null)
					{
						model.Materiel = row["Materiel"].ToString();
					}
				}
				if (row.Table.Columns.Contains("Unit"))
				{
					if (row["Unit"] != null && row["Unit"].ToString() != "")
					{
						model.Unit = int.Parse(row["Unit"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Qty"))
				{
					if (row["Qty"] != null && row["Qty"].ToString() != "")
					{
						model.Qty = decimal.Parse(row["Qty"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Price"))
				{
					if (row["Price"] != null && row["Price"].ToString() != "")
					{
						model.Price = decimal.Parse(row["Price"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Paid"))
				{
					if (row["Paid"] != null && row["Paid"].ToString() != "")
					{
						model.Paid = decimal.Parse(row["Paid"].ToString());
					}
				}
				if (row.Table.Columns.Contains("CheckOutDate"))
				{
					if (row["CheckOutDate"] != null && row["CheckOutDate"].ToString() != "")
					{
						model.CheckOutDate = DateTime.Parse(row["CheckOutDate"].ToString());
					}
				}
				if (row.Table.Columns.Contains("Arrears"))
				{
					if (row["Arrears"] != null && row["Arrears"].ToString() != "")
					{
						model.Arrears = decimal.Parse(row["Arrears"].ToString());
					}
				}
			}
			return model;
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XF.Model.Bill_DTOItems GetDetailModel(int Id)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("select  top 1 * from V_Bill_DTOItems ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			XF.Model.Bill_DTOItems model = new XF.Model.Bill_DTOItems();
			DataSet ds = SqlServerHelper.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}
		#endregion  ExtensionMethod
	}
}

